
import requests
import re
from urllib.parse import unquote
import json
from pprint import pprint
from DrissionPage import ChromiumPage

dp = ChromiumPage()

dp.listen.start('aweme/post/')

dp.get('https://www.douyin.com/user/MS4wLjABAAAASzaaMA1XZTvSolim9U_z5COMxQ7N8dWmi-2bXM3ujDg')

for page in range(1,30):
    print(f'正在爬取第{page}页数据')
    reps = dp.listen.wait()
    dp_json = reps.response.body
    for index in dp_json['aweme_list']:
        video_id = index['aweme_id']
        title = index['desc']
        title = re.sub(r'\n', '', title)
        print(video_id,title)
        """发送请求"""
        # 模拟浏览器
        headers = {
            'cookie':'douyin.com; UIFID_TEMP=3c3e9d4a635845249e00419877a3730e2149197a63ddb1d8525033ea2b3354c2df707c79f04f2b558d5b01fda9b552858ea16d95a803e3c953ed6d4711392e5a129688528b4517bcb221dbba1810efe1; fpk1=U2FsdGVkX19ayFA3oP5zw875hcbmH76pGGE2EWxdGFbxypAnrVT5Ue5xi8+OaebKp4CHZ5jp4qUlvxfI3MsV1A==; fpk2=f1f6b29a6cc1f79a0fea05b885aa33d0; bd_ticket_guard_client_web_domain=2; UIFID=3c3e9d4a635845249e00419877a3730e2149197a63ddb1d8525033ea2b3354c269f39f51d55d9ee7e65317921c61d8aa7b41f71c572c0de2680cad616385297f277a1fccdab52cd1ba641dcff5a5f6b358b35ab1a35793582c8f2294b93488c9351b9c0923ef1e9c6b32ff38ad9e60d02967a259b995692a33408fc49104fe26d933e4ec8a68374c6fc4d0a29e615a530ad34e449522f3ba3e98ae63346639a6; ttwid=1%7CUAJ8UryxyNaHIIjjzEpFL9EcXCcc2O_Zxg2X0FJl0m0%7C1722480873%7Ca6f3f416a0bdfe478122900632a6fa578bdac2db0329004f7f9c0bd45ea70f68; SEARCH_RESULT_LIST_TYPE=%22single%22; dy_swidth=1440; dy_sheight=960; d_ticket=7ff34110d68fdcd3de07d431adaf6eacab60a; passport_assist_user=CjxOOo7vGOPPDOpwCBHTEyTvd7WAsQBcEEko6EEbrCVbTyqBUiZ7SlOEZU8aJEME84pT_uDahJ2Z5qEabL4aSgo8WBW8HkyMy2JomI5f5NaICefA6De6KyYl4D5KxR2kccNdzutrSj44bQ3-RX-mF6RI7QTvxJ9qnx4bWmN3EPDB2Q0Yia_WVCABIgEDDknr8w%3D%3D; n_mh=q2ePUFht1OI_wY0AeX0k05bGK8iS_j8qHRQF38-jJQM; sso_uid_tt=cbebeb46e9c2ab13dc884c9b054ae57a; sso_uid_tt_ss=cbebeb46e9c2ab13dc884c9b054ae57a; toutiao_sso_user=77e4c9086974329b16cecb947be194ce; toutiao_sso_user_ss=77e4c9086974329b16cecb947be194ce; sid_ucp_sso_v1=1.0.0-KDgwZWRjYmJhZWFhNzM4NTA1ZWJhNTkzOWRiMDNjY2IzM2JlNDU2NDMKHwil1Pm67AIQj6f7tQYY7zEgDDC62ZXXBTgGQPQHSAYaAmxmIiA3N2U0YzkwODY5NzQzMjliMTZjZWNiOTQ3YmUxOTRjZQ; ssid_ucp_sso_v1=1.0.0-KDgwZWRjYmJhZWFhNzM4NTA1ZWJhNTkzOWRiMDNjY2IzM2JlNDU2NDMKHwil1Pm67AIQj6f7tQYY7zEgDDC62ZXXBTgGQPQHSAYaAmxmIiA3N2U0YzkwODY5NzQzMjliMTZjZWNiOTQ3YmUxOTRjZQ; passport_auth_status=73990dd818d083bf38efa63b1d3ec87a%2C; passport_auth_status_ss=73990dd818d083bf38efa63b1d3ec87a%2C; uid_tt=4953c06f75d68553866d66452a71dfeb; uid_tt_ss=4953c06f75d68553866d66452a71dfeb; sid_tt=efc54e9201447c15db5d6c415f0ebaa0; sessionid=efc54e9201447c15db5d6c415f0ebaa0; sessionid_ss=efc54e9201447c15db5d6c415f0ebaa0; is_staff_user=false; _bd_ticket_crypt_doamin=2; _bd_ticket_crypt_cookie=5223d8fadc6b2bf15e7970dbbe9a9f42; __security_server_data_status=1; sid_guard=efc54e9201447c15db5d6c415f0ebaa0%7C1723782045%7C5183989%7CTue%2C+15-Oct-2024+04%3A20%3A34+GMT; sid_ucp_v1=1.0.0-KGM4NjU4NzIzOWQ0YTU1NDJhYjRkOGIzZGI1OTcwYzRjYjhiODhlZjQKGQil1Pm67AIQnaf7tQYY7zEgDDgGQPQHSAQaAmhsIiBlZmM1NGU5MjAxNDQ3YzE1ZGI1ZDZjNDE1ZjBlYmFhMA; ssid_ucp_v1=1.0.0-KGM4NjU4NzIzOWQ0YTU1NDJhYjRkOGIzZGI1OTcwYzRjYjhiODhlZjQKGQil1Pm67AIQnaf7tQYY7zEgDDgGQPQHSAQaAmhsIiBlZmM1NGU5MjAxNDQ3YzE1ZGI1ZDZjNDE1ZjBlYmFhMA; live_use_vvc=%22false%22; my_rd=2; store-region=cn-zj; store-region-src=uid; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Atrue%2C%22volume%22%3A0.5%7D; stream_player_status_params=%22%7B%5C%22is_auto_play%5C%22%3A0%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A1%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A1%7D%22; hevc_supported=true; publish_badge_show_info=%220%2C0%2C0%2C1724501354099%22; s_v_web_id=verify_m0dr42m9_fev8WOuj_VKKR_4Qk2_ABeU_kTygrEw1OsGU; passport_csrf_token=e79c9db2566db25f7f268f1e333ba2bd; passport_csrf_token_default=e79c9db2566db25f7f268f1e333ba2bd; __live_version__=%221.1.2.3253%22; live_can_add_dy_2_desktop=%220%22; WallpaperGuide=%7B%22showTime%22%3A0%2C%22closeTime%22%3A0%2C%22showCount%22%3A0%2C%22cursor1%22%3A12%2C%22cursor2%22%3A0%7D; pwa2=%220%7C0%7C3%7C0%22; FOLLOW_NUMBER_YELLOW_POINT_INFO=%22MS4wLjABAAAAoWyNdRz43d9kx8t2aesJ04YUZoKEV8jR_z7JbGD3wLY%2F1724860800000%2F0%2F0%2F1724847059917%22; douyin.com; device_web_cpu_core=12; device_web_memory_size=8; architecture=amd64; csrf_session_id=6238ad960ac91a73fdd336e57c27a6c1; strategyABtestKey=%221724935755.595%22; biz_trace_id=6f24a8f9; xg_device_score=7.757734300177489; __ac_nonce=066d06e59006dd3357df0; __ac_signature=_02B4Z6wo00f01wC9YIwAAIDC3MT.OW63apMAnWQAAKcJ74; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1440%2C%5C%22screen_height%5C%22%3A960%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A12%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A7.8%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A50%7D%22; FOLLOW_LIVE_POINT_INFO=%22MS4wLjABAAAAoWyNdRz43d9kx8t2aesJ04YUZoKEV8jR_z7JbGD3wLY%2F1724947200000%2F0%2F0%2F1724936444537%22; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCR3hFMlFIMjhXVW9uRVlpWGlhT2tjNThZSXVnM3BSaTNLUVBjWGlMcnY4VHJPVGoxeVRmN2VMNGtWR2lYZkl4ZE5TOXB3eUpOKytVM21LS1J0Z3psSlU9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D; odin_tt=e933fc06a3b7483fc36c589c9ec8c9d2ba4fd80eb9c5397f782daff1d589d07de088a1b8a8ab490d0d975337b8a11fac; home_can_add_dy_2_desktop=%221%22; IsDouyinActive=false; passport_fe_beating_status=false',
            'referer':'https://www.douyin.com/user/MS4wLjABAAAAyvSk6NTcXdvmIXZkaY93-QHO2knHIzTaCOJ7bKaLvICyJP2dzlTtB0fWMra4S2Xk?from_tab_name=main&modal_id=7408436479833214246',
            'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
        }
        # 请求网址
        url = f'https://www.douyin.com/user/MS4wLjABAAAASzaaMA1XZTvSolim9U_z5COMxQ7N8dWmi-2bXM3ujDg?from_tab_name=main&modal_id={video_id}'
        # 发送请求
        response = requests.get(url=url, headers=headers)
        """获取数据"""
        # 获取网页的文本数据
        html = response.text
        # 提取视频信息
        info = re.findall('<script id="RENDER_DATA" type="application/json">(.*?)</script>', html)[0]
        # print(info)
        """解析数据"""
        # 解码数据
        json_str = unquote(info)
        print(json_str)
        json_data = json.loads(json_str)
        # pprint(json_data)
        # 提取视频链接
        bitRateList = json_data['app']['videoDetail']['video']['bitRateList']
        if bitRateList:
            playAddrList = bitRateList[0]['playAddr']
            if playAddrList:
                video_url = 'https:' + playAddrList[-1]['src']
            else:
                print('playAddrList is empty')
                continue
        else:
            print('bitRateList is empty')
            continue
        # 提取视频标题
        #title = json_data['app']['videoDetail']['desc']
        """保存数据"""
        # 获取视频内容
        video_content = requests.get(video_url, headers=headers).content
        # 写入磁盘
        with open('video\\' + title + '.mp4', 'wb') as f:
            f.write(video_content)

    dp.scroll.to_bottom()